package com.cicc.beergamevirtualsimulationsystem.common.webSocket.handler.impl;

import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.cicc.beergamevirtualsimulationsystem.common.webSocket.config.WebSocketMessageSender;
import com.cicc.beergamevirtualsimulationsystem.common.webSocket.handler.JsonMessageHandler;
import com.cicc.beergamevirtualsimulationsystem.common.webSocket.message.Impl.RoundInitFinishJsonWebSocketMessage;
import com.cicc.beergamevirtualsimulationsystem.common.webSocket.message.WebSocketMessageTypeEnum;
import com.cicc.beergamevirtualsimulationsystem.entity.InitializeEntity;
import com.cicc.beergamevirtualsimulationsystem.entity.RoomInfoManager;
import com.cicc.beergamevirtualsimulationsystem.model.roleBinding.RoleBindingReq;
import com.cicc.beergamevirtualsimulationsystem.service.RecordService;
import com.cicc.beergamevirtualsimulationsystem.service.RoomService;
import com.fasterxml.jackson.annotation.JsonFormat;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;

import java.util.List;

@Component
public class RoundInitFinishJsonMessageHandler implements JsonMessageHandler<RoundInitFinishJsonWebSocketMessage> {

    @Resource
    private RecordService recordService;
    @Resource
    private RoomService roomService;
    @Override
    public void handle(WebSocketSession session, RoundInitFinishJsonWebSocketMessage message) {
        String roomId = (String)session.getAttributes().get("roomId");
        String userId = (String)session.getAttributes().get("userId");
        InitializeEntity bean = JSONUtil.toBean(message.getMessage(), InitializeEntity.class);
        if (bean.getIsFirst()==1) {
            if (RoomInfoManager.isFull(roomId)) {
                List<RoleBindingReq> roleBindingReqs = RoomInfoManager.savePlayerRole(roomId);
                roomService.startGame(roomId,userId,roleBindingReqs);
                recordService.initRecord(bean.getRoomId(),bean.getPeriod(),bean.getOrderNum());
                System.out.println("第一回合初始化数据："+bean);
                WebSocketMessageSender.send(session,new RoundInitFinishJsonWebSocketMessage("0"));
            }else {
                WebSocketMessageSender.send(session,new RoundInitFinishJsonWebSocketMessage("-1"));
            }
        }else {
            //json转对象
//            recordService.settle(bean.getRoomId(),bean.getPeriod()-1);
//            recordService.initRecord(bean.getRoomId(),bean.getPeriod(),bean.getOrderNum());
//            System.out.println("回合初始化数据："+bean);
            RoomInfoManager.changeCustomerOrder(roomId,session,bean.getOrderNum());
            WebSocketMessageSender.sendMessageToRoom(roomId,new RoundInitFinishJsonWebSocketMessage("1"));

        }


    }

    @Override
    public String type() {
        return WebSocketMessageTypeEnum.ROUND_INIT_FINISH.getValue();
    }

    @Override
    public Class<RoundInitFinishJsonWebSocketMessage> getMessageClass() {
        return RoundInitFinishJsonWebSocketMessage.class;
    }
}
